function [res_mat,vis_resp,run_resp,blink_resp,eye_resp,fdbk_resp,blink_dark_resp,eye_dark_resp,run_dark_resp,fb_snip,pb_snip,other]=meta_get_cell_type_onset(proj_meta,cur_site)
order={};
exp_inds={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
    if length(nbr_frames)>5
        for xnd=1:length(nbr_frames)
            if nbr_frames(xnd)==5000
                if xnd==length(nbr_frames)
                    order{tnd}(xnd)=3;
                else
                    order{tnd}(xnd)=1;
                end
            else
                order{tnd}(xnd)=2;
            end
        end
        ord_ind=1:length(order{tnd});
        order{tnd}(max(ord_ind(order{tnd}==2))+1:end)=3;
    else
        order{tnd}=[1 2 1 2 3];
    end
    exp_inds{tnd}(1,1)=1;
    exp_inds{tnd}(1,2)=nbr_frames(1);
    for fnd=2:length(order{tnd})
        exp_inds{tnd}(fnd,1)=exp_inds{tnd}(fnd-1,2)+1;
        exp_inds{tnd}(fnd,2)=sum(nbr_frames(1:fnd));
    end
end

shad_all={};
vis_fl_all={};
delta_all={};
pert_num={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    cur_pert_num=[];
    ps_id=proj_meta(cur_site).rd(1,tnd).ps_id;
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
    pupil_pos=proj_meta(cur_site).rd(1,tnd).pupil_pos;
    shad=[];
    vis_fl=[];
    cnt=1;
    for bnd=1:length(order{tnd})
        if order{tnd}(bnd)==1
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if tmp_runM(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==0 %feedback
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif tmp_runM(fnd)==1 & logical(tmp_ps_id(fnd))==1 %feedback mismatch
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        elseif order{tnd}(bnd)==2
            tmp_runM=runM(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_ps_id=ps_id(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            tmp_runP=runP(exp_inds{tnd}(bnd,1):exp_inds{tnd}(bnd,2));
            for fnd=1:length(tmp_runM)
                if logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==0
                    shad(cnt)=0;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=5;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=6;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==0 & tmp_runP(fnd)==1
                    shad(cnt)=2;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    shad(cnt)=1;
                    vis_fl(cnt)=1;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==0 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0 | logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==0
                    shad(cnt)=3;
                    vis_fl(cnt)=0;
                    cnt=cnt+1;
                elseif logical(tmp_ps_id(fnd))==1 & tmp_runM(fnd)==1 & tmp_runP(fnd)==1
                    if tmp_ps_id(fnd)==7
                        shad(cnt)=3;
                        vis_fl(cnt)=0;
                        cnt=cnt+1;
                    else
                        cur_pert_num(cnt)=tmp_ps_id(fnd);
                        shad(cnt)=4;
                        vis_fl(cnt)=1;
                        cnt=cnt+1;
                    end
                end
            end
        end
    end
    shad_all{tnd}=shad;
    vis_fl(length(vis_fl):length(runM))=0;
    vis_fl_all{tnd}=vis_fl;
    delta=sqrt(diff(pupil_pos(1,:)).^2+diff(pupil_pos(2,:)).^2);
    delta(end+1)=delta(end);
    delta_all{tnd}=delta;
    pert_num{tnd}=cur_pert_num;
end

all_act={};
pl_cells=[];
mean_fluo=[];
for tnd=1:size(proj_meta(cur_site).rd,2)
    cnt=0;
    for vnd=1:size(proj_meta(cur_site).rd,1)
        all_act{tnd}(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),:)=proj_meta(cur_site).rd(vnd,tnd).act;
        mean_fluo(cnt+1:cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1),tnd)=mean(proj_meta(cur_site).rd(vnd,tnd).act,2);
        cnt=cnt+size(proj_meta(cur_site).rd(vnd,tnd).act,1);
        pl_cells(vnd)=cnt;
    end
end

fb_snip={};
pb_snip={};
fb_av={};
pb_av={};
for tnd=1:size(proj_meta(cur_site).rd,2)
    nbr_frames=proj_meta(cur_site).rd(1,tnd).nbr_frames;
    stim_id=proj_meta(cur_site).rd(1,tnd).ps_id;
    runP=proj_meta(cur_site).rd(1,tnd).velP_smoothed>0.005;
    runM=proj_meta(cur_site).rd(1,tnd).velM_smoothed>0.005;
    runM_raw=proj_meta(cur_site).rd(1,tnd).velM_smoothed;
    if nargin==3
        for lnd=1:length(cells)
            act=all_act{tnd}(cells(lnd),:);
            [fb_av{lnd,tnd},pb_av{lnd,tnd},fb_snip{lnd,tnd},pb_snip{lnd,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
        end
    else
        for lnd=1:size(all_act{1},1)
            act=all_act{tnd}(lnd,:);
            [fb_av{lnd,tnd},pb_av{lnd,tnd},fb_snip{lnd,tnd},pb_snip{lnd,tnd}]=pert_ana_meta(nbr_frames,stim_id,runM,runP,runM_raw,act);
        end
    end
end

[vis_snips,run_snips,run_snips_dark,fdbk_snips]=meta_analysis_find_onset_snips(proj_meta,cur_site,vis_fl_all,order,exp_inds,shad_all);
[blink_snips,blink_snips_dark,eye_snips,eye_snips_dark]=meta_analysis_eye_onset(proj_meta,cur_site,order,exp_inds,shad_all);

for gnd=1:size(proj_meta(cur_site).rd,2)
    test_mat=[];
    cnt=1;
    for tnd=1:size(vis_snips{gnd},1)
        test_mat(cnt,:)=vis_snips{gnd}(tnd,1):vis_snips{gnd}(tnd,2);
        cnt=cnt+1;
    end
    for tnd=1:size(run_snips{gnd},1)
        test_mat(cnt,:)=run_snips{gnd}(tnd,1):run_snips{gnd}(tnd,2);
        cnt=cnt+1;
    end
    for tnd=1:size(fdbk_snips{gnd},1)
        test_mat(cnt,:)=fdbk_snips{gnd}(tnd,1):fdbk_snips{gnd}(tnd,2);
        cnt=cnt+1;
    end
    %     for tnd=1:size(ffm_snips{gnd},1)
    %         test_mat(cnt,:)=ffm_snips{gnd}(tnd,1):ffm_snips{gnd}(tnd,2);
    %         cnt=cnt+1;
    %     end
    rem=[];
    for tnd=1:size(blink_snips{gnd},1)
        cur_check=logical(sum(ismember(test_mat,[blink_snips{gnd}(tnd,1):blink_snips{gnd}(tnd,2)]),1));
        if sum(cur_check)>11
            rem(tnd,:)=1;
        else
            rem(tnd,:)=0;
        end
    end
    blink_snips{gnd}(logical(rem),:)=[];
    rem=[];
    for tnd=1:size(eye_snips{gnd},1)
        cur_check=logical(sum(ismember(test_mat,[eye_snips{gnd}(tnd,1):eye_snips{gnd}(tnd,2)]),1));
        if sum(cur_check)>11
            rem(tnd,:)=1;
        else
            rem(tnd,:)=0;
        end
    end
    eye_snips{gnd}(logical(rem),:)=[];
end

for gnd=1:size(proj_meta(cur_site).rd,2)
    test_mat=[];
    cnt=1;
    for tnd=1:size(run_snips_dark{gnd},1)
        test_mat(cnt,:)=run_snips_dark{gnd}(tnd,1):run_snips_dark{gnd}(tnd,2);
        cnt=cnt+1;
    end
    rem=[];
    if ~isempty(blink_snips_dark{gnd})
        for tnd=1:size(blink_snips_dark{gnd},1)
            cur_check=logical(sum(ismember(test_mat,[blink_snips_dark{gnd}(tnd,1):blink_snips_dark{gnd}(tnd,2)]),1));
            if sum(cur_check)>11
                rem(tnd,:)=1;
            else
                rem(tnd,:)=0;
            end
        end
        blink_snips_dark{gnd}(logical(rem),:)=[];
    end
    rem=[];
    if ~isempty(eye_snips_dark{gnd})
        for tnd=1:size(eye_snips_dark{gnd},1)
            cur_check=logical(sum(ismember(test_mat,[eye_snips_dark{gnd}(tnd,1):eye_snips_dark{gnd}(tnd,2)]),1));
            if sum(cur_check)>11
                rem(tnd,:)=1;
            else
                rem(tnd,:)=0;
            end
        end
        eye_snips_dark{gnd}(logical(rem),:)=[];
    end
end

vis_resp={};
vis_mean={};
run_resp={};
run_mean={};
blink_resp={};
blink_mean={};
eye_resp={};
eye_mean={};
% ffm_resp={};
% ffm_mean={};

run_dark_resp={};
run_dark_mean={};
blink_dark_resp={};
blink_dark_mean={};
eye_dark_resp={};
eye_dark_mean={};
fdbk_resp={};
fdbk_mean={};

res_mat=[];
for ind=1:size(all_act{1},1)
    for tnd=1:size(proj_meta(cur_site).rd,2)
        cur_act=all_act{tnd}(ind,:);
        if ~isempty(vis_snips{tnd})
            for hnd=1:size(vis_snips{tnd},1)
                vis_resp{ind,tnd}(hnd,:)=cur_act(vis_snips{tnd}(hnd,1):vis_snips{tnd}(hnd,2));
            end
            vis_mean{tnd}(ind,:)=mean(vis_resp{ind,tnd},1);
            res_mat(ind,tnd,1)=mean(vis_mean{tnd}(ind,15:20))-mean(vis_mean{tnd}(ind,5:10));
        else
            vis_resp{ind,tnd}=[];
            vis_mean{tnd}=[];
        end
        if ~isempty(run_snips{tnd})
            for hnd=1:size(run_snips{tnd},1)
                run_resp{ind,tnd}(hnd,:)=cur_act(run_snips{tnd}(hnd,1):run_snips{tnd}(hnd,2));
            end
            run_mean{tnd}(ind,:)=mean(run_resp{ind,tnd},1);
            res_mat(ind,tnd,2)=mean(run_mean{tnd}(ind,15:20))-mean(run_mean{tnd}(ind,5:10));
        else
            run_resp{ind,tnd}=[];
            run_mean{tnd}=[];
        end
        if ~isempty(blink_snips{tnd})
            for hnd=1:size(blink_snips{tnd},1)
                blink_resp{ind,tnd}(hnd,:)=cur_act(blink_snips{tnd}(hnd,1):blink_snips{tnd}(hnd,2));
            end
            blink_mean{tnd}(ind,:)=mean(blink_resp{ind,tnd},1);
            res_mat(ind,tnd,3)=mean(blink_mean{tnd}(ind,15:20))-mean(blink_mean{tnd}(ind,5:10));
        else
            blink_resp{ind,tnd}=[];
            blink_mean{tnd}=[];
        end
        if ~isempty(eye_snips{tnd})
            for hnd=1:size(eye_snips{tnd},1)
                eye_resp{ind,tnd}(hnd,:)=cur_act(eye_snips{tnd}(hnd,1):eye_snips{tnd}(hnd,2));
            end
            eye_mean{tnd}(ind,:)=mean(eye_resp{ind,tnd},1);
            res_mat(ind,tnd,4)=mean(eye_mean{tnd}(ind,15:20))-mean(eye_mean{tnd}(ind,5:10));
        else
            eye_resp{ind,tnd}=[];
            eye_mean{tnd}=[];
        end
        if ~isempty(fdbk_snips{tnd})
            for hnd=1:size(fdbk_snips{tnd},1)
                fdbk_resp{ind,tnd}(hnd,:)=cur_act(fdbk_snips{tnd}(hnd,1):fdbk_snips{tnd}(hnd,2));
            end
            fdbk_mean{tnd}(ind,:)=mean(fdbk_resp{ind,tnd},1);
            res_mat(ind,tnd,5)=mean(fdbk_mean{tnd}(ind,15:20))-mean(fdbk_mean{tnd}(ind,5:10));
        else
            fdbk_resp{ind,tnd}=[];
            fdbk_mean{tnd}=[];
        end
        %         for hnd=1:size(ffm_snips{tnd},1)
        %             ffm_resp{ind,tnd}(hnd,:)=cur_act(ffm_snips{tnd}(hnd,1):ffm_snips{tnd}(hnd,2));
        %         end
        %         ffm_mean{tnd}(ind,:)=mean(ffm_resp{ind,tnd},1);
        %         res_mat(ind,tnd,6)=mean(ffm_mean{tnd}(ind,15:20))-mean(ffm_mean{tnd}(ind,5:10));
        if ~isempty(blink_snips_dark{tnd})
            for hnd=1:size(blink_snips_dark{tnd},1)
                blink_dark_resp{ind,tnd}(hnd,:)=cur_act(blink_snips_dark{tnd}(hnd,1):blink_snips_dark{tnd}(hnd,2));
            end
            blink_dark_mean{tnd}(ind,:)=mean(blink_dark_resp{ind,tnd},1);
            res_mat(ind,tnd,7)=mean(blink_dark_mean{tnd}(ind,15:20))-mean(blink_dark_mean{tnd}(ind,5:10));
        else
            blink_dark_resp{ind,tnd}=[];
            blink_dark_mean{tnd}=[];
        end
        if ~isempty(eye_snips_dark{tnd})
            for hnd=1:size(eye_snips_dark{tnd},1)
                eye_dark_resp{ind,tnd}(hnd,:)=cur_act(eye_snips_dark{tnd}(hnd,1):eye_snips_dark{tnd}(hnd,2));
            end
            eye_dark_mean{tnd}(ind,:)=mean(eye_dark_resp{ind,tnd},1);
            res_mat(ind,tnd,8)=mean(eye_dark_mean{tnd}(ind,15:20))-mean(eye_dark_mean{tnd}(ind,5:10));
        else
            eye_dark_resp{ind,tnd}=[];
            eye_dark_mean{tnd}=[];
        end
        if ~isempty(run_snips_dark{tnd})
            for hnd=1:size(run_snips_dark{tnd},1)
                run_dark_resp{ind,tnd}(hnd,:)=cur_act(run_snips_dark{tnd}(hnd,1):run_snips_dark{tnd}(hnd,2));
            end
            run_dark_mean{tnd}(ind,:)=mean(run_dark_resp{ind,tnd},1);
            res_mat(ind,tnd,9)=mean(run_dark_mean{tnd}(ind,15:20))-mean(run_dark_mean{tnd}(ind,5:10));
        else
            run_dark_resp{ind,tnd}=[];
            run_dark_mean{tnd}=[];
        end
    end
end

other=struct;
for ind=1:size(proj_meta(cur_site).rd,2)
    other(ind).cur_runM=proj_meta(cur_site).rd(1,ind).velM_smoothed;
    other(ind).cur_runM_ind=proj_meta(cur_site).rd(1,ind).velM_smoothed>0.005;
    other(ind).cur_runP=proj_meta(cur_site).rd(1,ind).velP_smoothed;
    other(ind).cur_runP_ind=proj_meta(cur_site).rd(1,ind).velP_smoothed>0.005;
    other(ind).cur_vis=vis_fl_all{ind};
    other(ind).cur_shad=shad_all{ind};
    for hnd=1:size(vis_snips{ind},1)
        other(ind).vis.runM(hnd,:)=other(ind).cur_runM(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
        other(ind).vis.runM_ind(hnd,:)=other(ind).cur_runM_ind(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
        other(ind).vis.runP(hnd,:)=other(ind).cur_runP(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
        other(ind).vis.runP_ind(hnd,:)=other(ind).cur_runP_ind(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
        other(ind).vis.vis(hnd,:)=other(ind).cur_vis(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
        other(ind).vis.shad(hnd,:)=other(ind).cur_shad(vis_snips{ind}(hnd,1):vis_snips{ind}(hnd,2));
    end
    for hnd=1:size(run_snips{ind},1)
        other(ind).run.runM(hnd,:)=other(ind).cur_runM(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
        other(ind).run.runM_ind(hnd,:)=other(ind).cur_runM_ind(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
        other(ind).run.runP(hnd,:)=other(ind).cur_runP(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
        other(ind).run.runP_ind(hnd,:)=other(ind).cur_runP_ind(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
        other(ind).run.vis(hnd,:)=other(ind).cur_vis(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
        other(ind).run.shad(hnd,:)=other(ind).cur_shad(run_snips{ind}(hnd,1):run_snips{ind}(hnd,2));
    end
    for hnd=1:size(fdbk_snips{ind},1)
        other(ind).fdbk.runM(hnd,:)=other(ind).cur_runM(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
        other(ind).fdbk.runM_ind(hnd,:)=other(ind).cur_runM_ind(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
        other(ind).fdbk.runP(hnd,:)=other(ind).cur_runP(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
        other(ind).fdbk.runP_ind(hnd,:)=other(ind).cur_runP_ind(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
        other(ind).fdbk.vis(hnd,:)=other(ind).cur_vis(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
        other(ind).fdbk.shad(hnd,:)=other(ind).cur_shad(fdbk_snips{ind}(hnd,1):fdbk_snips{ind}(hnd,2));
    end
end
